package com.example.common.util;

import org.springframework.http.HttpStatus;

import java.util.HashMap;

/**
 * @Author: 582895699@qq.com
 * @Desctription: TODO
 * @Date: Created in 2021/12/2 0:41
 */
public class Resp extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public static final String CODE_TAG = "code";
    public static final String MSG_TAG = "msg";
    public static final String DATA_TAG = "data";

    public Resp() {
        put(CODE_TAG, HttpStatus.OK.value());
        put(MSG_TAG, HttpStatus.OK.getReasonPhrase());
    }

    public static Resp ok() {
        return new Resp();
    }

    public static Resp ok(Object data) {
        Resp resp = new Resp();
        resp.put(DATA_TAG, data);
        return resp;
    }

    public static Resp ok(String field, Object value) {
        Resp resp = new Resp();
        resp.put(field, value);
        return resp;
    }

    public static Resp fail(Integer code, String msg) {
        Resp resp = new Resp();
        resp.put(CODE_TAG, code);
        resp.put(MSG_TAG, msg);
        return resp;
    }

    public static Resp fail() {
        Resp resp = new Resp();
        resp.put(CODE_TAG, HttpStatus.INTERNAL_SERVER_ERROR.value());
        resp.put(MSG_TAG, HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        return resp;
    }

    public static Resp fail(String msg) {
        return Resp.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    @Override
    public Object put(String key, Object value) {
        return super.put(key, value);
    }
}
